package com.cencat.common.enums;

/**
 * 支付状态枚举
 * 
 * @author cencat
 * @since 2024-01-01
 */
public enum PaymentStatus {

    /**
     * 待支付
     */
    PENDING(1, "待支付"),

    /**
     * 支付中
     */
    PROCESSING(2, "支付中"),

    /**
     * 支付成功
     */
    SUCCESS(3, "支付成功"),

    /**
     * 支付失败
     */
    FAILED(4, "支付失败"),

    /**
     * 支付取消
     */
    CANCELLED(5, "支付取消"),

    /**
     * 支付超时
     */
    TIMEOUT(6, "支付超时"),

    /**
     * 退款中
     */
    REFUNDING(7, "退款中"),

    /**
     * 退款成功
     */
    REFUNDED(8, "退款成功"),

    /**
     * 退款失败
     */
    REFUND_FAILED(9, "退款失败"),

    /**
     * 部分退款
     */
    PARTIAL_REFUNDED(10, "部分退款");

    /**
     * 状态码
     */
    private final Integer code;

    /**
     * 状态描述
     */
    private final String description;

    /**
     * 构造函数
     * 
     * @param code 状态码
     * @param description 状态描述
     */
    PaymentStatus(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 获取状态码
     * 
     * @return 状态码
     */
    public Integer getCode() {
        return code;
    }

    /**
     * 获取状态描述
     * 
     * @return 状态描述
     */
    public String getDescription() {
        return description;
    }

    /**
     * 根据状态码获取枚举
     * 
     * @param code 状态码
     * @return 支付状态枚举
     */
    public static PaymentStatus getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (PaymentStatus status : PaymentStatus.values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        return null;
    }

    /**
     * 判断是否为成功状态
     * 
     * @return 是否成功
     */
    public boolean isSuccess() {
        return this == SUCCESS;
    }

    /**
     * 判断是否为失败状态
     * 
     * @return 是否失败
     */
    public boolean isFailed() {
        return this == FAILED || this == CANCELLED || this == TIMEOUT;
    }

    /**
     * 判断是否为进行中状态
     * 
     * @return 是否进行中
     */
    public boolean isProcessing() {
        return this == PENDING || this == PROCESSING || this == REFUNDING;
    }

    /**
     * 判断是否为终态
     * 
     * @return 是否为终态
     */
    public boolean isFinal() {
        return this == SUCCESS || this == FAILED || this == CANCELLED || 
               this == TIMEOUT || this == REFUNDED || this == REFUND_FAILED || this == PARTIAL_REFUNDED;
    }

    /**
     * 判断是否可以退款
     * 
     * @return 是否可以退款
     */
    public boolean canRefund() {
        return this == SUCCESS;
    }

    /**
     * 判断是否可以取消
     * 
     * @return 是否可以取消
     */
    public boolean canCancel() {
        return this == PENDING || this == PROCESSING;
    }

    /**
     * 判断是否已退款
     * 
     * @return 是否已退款
     */
    public boolean isRefunded() {
        return this == REFUNDED || this == PARTIAL_REFUNDED;
    }

    /**
     * 判断是否需要重试
     * 
     * @return 是否需要重试
     */
    public boolean needRetry() {
        return this == FAILED && this != CANCELLED && this != TIMEOUT;
    }
}